package com.demo.webmagic.task;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import com.demo.common.model._MappingKit;
import com.demo.webmagic.pageProcessor.PageProcessorAnxiaomo;
import com.demo.webmagic.pageProcessor.PageProcessorGoubanjia;
import com.demo.webmagic.pageProcessor.PageProcessorHaodaili;
import com.demo.webmagic.pageProcessor.PageProcessorKuaidaili;
import com.demo.webmagic.pipeline.PipelineProxyIp;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.selenium.SeleniumDownloader;

public class TaskProxyip {

	public static void main(String[] args) {
		PropKit.use("a_little_config.txt");
		DruidPlugin dp = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"));
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		_MappingKit.mapping(arp);
		dp.start();
		arp.start();
		
		/*List<Proxyip> proxyipList = Proxyip.dao.find("SELECT * FROM proxyip");
		for (Proxyip proxyip : proxyipList) {
			proxyip.delete();
		}*/
		
		//快代理
		String[] kuaidailiUrlList = new String[3];
		for (int i = 1; i <= 3; i++) {
			kuaidailiUrlList[i-1] = "http://www.kuaidaili.com/free/inha/" + i;
		}
		Spider.create(new PageProcessorKuaidaili()).addUrl(kuaidailiUrlList).addPipeline(new PipelineProxyIp()).start();
		//好代理
		String[] haodailiUrlList = new String[10];
		for (int i = 1; i <= 10; i++) {
			haodailiUrlList[i-1] = "http://www.haodailiip.com/guonei/" + i;
		}
		Spider.create(new PageProcessorHaodaili()).addUrl(haodailiUrlList).addPipeline(new PipelineProxyIp()).start();
	
		//安小莫
		Spider.create(new PageProcessorAnxiaomo()).addPipeline(new PipelineProxyIp()).addUrl("http://www.66ip.cn/nmtq.php?getnum=800&isp=0&anonymoustype=4&start=&ports=&export=&ipaddress=&area=1&proxytype=0&api=66ip").start();
	
		//狗搬家
		try {
			Spider.create(new PageProcessorGoubanjia()).addPipeline(new PipelineProxyIp()).setDownloader(new SeleniumDownloader(URLDecoder.decode(PageProcessorAnxiaomo.class.getClass().getResource("/").getPath(), "utf-8") + "chromedriver.exe")).start();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
}
